package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.FeedTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.FodderStorageDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTakeDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTypeDAO;
import cn.edu.scau.cmi.hombio.dao.UnitDAO;

import cn.edu.scau.cmi.hombio.domain.FeedTaskQuality;
import cn.edu.scau.cmi.hombio.domain.FodderStorage;
import cn.edu.scau.cmi.hombio.domain.FodderTake;
import cn.edu.scau.cmi.hombio.domain.FodderType;
import cn.edu.scau.cmi.hombio.domain.Unit;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for FodderType entities
 * 
 */

@Service("FodderTypeService")

@Transactional
public class FodderTypeServiceImpl implements FodderTypeService {

	/**
	 * DAO injected by Spring that manages FeedTaskQuality entities
	 * 
	 */
	@Autowired
	private FeedTaskQualityDAO feedTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages FodderStorage entities
	 * 
	 */
	@Autowired
	private FodderStorageDAO fodderStorageDAO;

	/**
	 * DAO injected by Spring that manages FodderTake entities
	 * 
	 */
	@Autowired
	private FodderTakeDAO fodderTakeDAO;

	/**
	 * DAO injected by Spring that manages FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeDAO fodderTypeDAO;

	/**
	 * DAO injected by Spring that manages Unit entities
	 * 
	 */
	@Autowired
	private UnitDAO unitDAO;

	/**
	 * Instantiates a new FodderTypeServiceImpl.
	 *
	 */
	public FodderTypeServiceImpl() {
	}

	/**
	 * Delete an existing FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public FodderType deleteFodderTypeFeedTaskQualities(Integer foddertype_id, Integer related_feedtaskqualities_id) {
		FeedTaskQuality related_feedtaskqualities = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(related_feedtaskqualities_id, -1, -1);

		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id, -1, -1);

		related_feedtaskqualities.setFodderType(null);
		foddertype.getFeedTaskQualities().remove(related_feedtaskqualities);

		feedTaskQualityDAO.remove(related_feedtaskqualities);
		feedTaskQualityDAO.flush();

		return foddertype;
	}

	/**
	 * Save an existing FodderTake entity
	 * 
	 */
	@Transactional
	public FodderType saveFodderTypeFodderTakes(Integer id, FodderTake related_foddertakes) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(id, -1, -1);
		FodderTake existingfodderTakes = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakes.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderTakes != null) {
			existingfodderTakes.setId(related_foddertakes.getId());
			existingfodderTakes.setQuantity(related_foddertakes.getQuantity());
			existingfodderTakes.setTime(related_foddertakes.getTime());
			existingfodderTakes.setMemo(related_foddertakes.getMemo());
			related_foddertakes = existingfodderTakes;
		}

		related_foddertakes.setFodderType(foddertype);
		foddertype.getFodderTakes().add(related_foddertakes);
		related_foddertakes = fodderTakeDAO.store(related_foddertakes);
		fodderTakeDAO.flush();

		foddertype = fodderTypeDAO.store(foddertype);
		fodderTypeDAO.flush();

		return foddertype;
	}

	/**
	 * Load an existing FodderType entity
	 * 
	 */
	@Transactional
	public Set<FodderType> loadFodderTypes() {
		return fodderTypeDAO.findAllFodderTypes();
	}

	/**
	 * Delete an existing FodderStorage entity
	 * 
	 */
	@Transactional
	public FodderType deleteFodderTypeFodderStorages(Integer foddertype_id, Integer related_fodderstorages_id) {
		FodderStorage related_fodderstorages = fodderStorageDAO.findFodderStorageByPrimaryKey(related_fodderstorages_id, -1, -1);

		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id, -1, -1);

		related_fodderstorages.setFodderType(null);
		foddertype.getFodderStorages().remove(related_fodderstorages);

		fodderStorageDAO.remove(related_fodderstorages);
		fodderStorageDAO.flush();

		return foddertype;
	}

	/**
	 * Save an existing Unit entity
	 * 
	 */
	@Transactional
	public FodderType saveFodderTypeUnit(Integer id, Unit related_unit) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(id, -1, -1);
		Unit existingunit = unitDAO.findUnitByPrimaryKey(related_unit.getId());

		// copy into the existing record to preserve existing relationships
		if (existingunit != null) {
			existingunit.setId(related_unit.getId());
			existingunit.setName(related_unit.getName());
			existingunit.setMemo(related_unit.getMemo());
			related_unit = existingunit;
		} else {
			related_unit = unitDAO.store(related_unit);
			unitDAO.flush();
		}

		foddertype.setUnit(related_unit);
		related_unit.getFodderTypes().add(foddertype);
		foddertype = fodderTypeDAO.store(foddertype);
		fodderTypeDAO.flush();

		related_unit = unitDAO.store(related_unit);
		unitDAO.flush();

		return foddertype;
	}

	/**
	 * Save an existing FodderType entity
	 * 
	 */
	@Transactional
	public void saveFodderType(FodderType foddertype) {
		FodderType existingFodderType = fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype.getId());

		if (existingFodderType != null) {
			if (existingFodderType != foddertype) {
				existingFodderType.setId(foddertype.getId());
				existingFodderType.setName(foddertype.getName());
				existingFodderType.setSupply(foddertype.getSupply());
				existingFodderType.setMemo(foddertype.getMemo());
			}
			foddertype = fodderTypeDAO.store(existingFodderType);
		} else {
			foddertype = fodderTypeDAO.store(foddertype);
		}
		fodderTypeDAO.flush();
	}

	/**
	 * Delete an existing FodderType entity
	 * 
	 */
	@Transactional
	public void deleteFodderType(FodderType foddertype) {
		fodderTypeDAO.remove(foddertype);
		fodderTypeDAO.flush();
	}

	/**
	 * Save an existing FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public FodderType saveFodderTypeFeedTaskQualities(Integer id, FeedTaskQuality related_feedtaskqualities) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(id, -1, -1);
		FeedTaskQuality existingfeedTaskQualities = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(related_feedtaskqualities.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfeedTaskQualities != null) {
			existingfeedTaskQualities.setId(related_feedtaskqualities.getId());
			existingfeedTaskQualities.setMemo(related_feedtaskqualities.getMemo());
			related_feedtaskqualities = existingfeedTaskQualities;
		}

		related_feedtaskqualities.setFodderType(foddertype);
		foddertype.getFeedTaskQualities().add(related_feedtaskqualities);
		related_feedtaskqualities = feedTaskQualityDAO.store(related_feedtaskqualities);
		feedTaskQualityDAO.flush();

		foddertype = fodderTypeDAO.store(foddertype);
		fodderTypeDAO.flush();

		return foddertype;
	}

	/**
	 * Save an existing FodderStorage entity
	 * 
	 */
	@Transactional
	public FodderType saveFodderTypeFodderStorages(Integer id, FodderStorage related_fodderstorages) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(id, -1, -1);
		FodderStorage existingfodderStorages = fodderStorageDAO.findFodderStorageByPrimaryKey(related_fodderstorages.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderStorages != null) {
			existingfodderStorages.setId(related_fodderstorages.getId());
			existingfodderStorages.setQuantity(related_fodderstorages.getQuantity());
			existingfodderStorages.setTime(related_fodderstorages.getTime());
			related_fodderstorages = existingfodderStorages;
		}

		related_fodderstorages.setFodderType(foddertype);
		foddertype.getFodderStorages().add(related_fodderstorages);
		related_fodderstorages = fodderStorageDAO.store(related_fodderstorages);
		fodderStorageDAO.flush();

		foddertype = fodderTypeDAO.store(foddertype);
		fodderTypeDAO.flush();

		return foddertype;
	}

	/**
	 * Delete an existing Unit entity
	 * 
	 */
	@Transactional
	public FodderType deleteFodderTypeUnit(Integer foddertype_id, Integer related_unit_id) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id, -1, -1);
		Unit related_unit = unitDAO.findUnitByPrimaryKey(related_unit_id, -1, -1);

		foddertype.setUnit(null);
		related_unit.getFodderTypes().remove(foddertype);
		foddertype = fodderTypeDAO.store(foddertype);
		fodderTypeDAO.flush();

		related_unit = unitDAO.store(related_unit);
		unitDAO.flush();

		unitDAO.remove(related_unit);
		unitDAO.flush();

		return foddertype;
	}

	/**
	 * Return a count of all FodderType entity
	 * 
	 */
	@Transactional
	public Integer countFodderTypes() {
		return ((Long) fodderTypeDAO.createQuerySingleResult("select count(o) from FodderType o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing FodderTake entity
	 * 
	 */
	@Transactional
	public FodderType deleteFodderTypeFodderTakes(Integer foddertype_id, Integer related_foddertakes_id) {
		FodderTake related_foddertakes = fodderTakeDAO.findFodderTakeByPrimaryKey(related_foddertakes_id, -1, -1);

		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype_id, -1, -1);

		related_foddertakes.setFodderType(null);
		foddertype.getFodderTakes().remove(related_foddertakes);

		fodderTakeDAO.remove(related_foddertakes);
		fodderTakeDAO.flush();

		return foddertype;
	}

	/**
	 * Return all FodderType entity
	 * 
	 */
	@Transactional
	public List<FodderType> findAllFodderTypes(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<FodderType>(fodderTypeDAO.findAllFodderTypes(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public FodderType findFodderTypeByPrimaryKey(Integer id) {
		return fodderTypeDAO.findFodderTypeByPrimaryKey(id);
	}
}
